#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time    : 2018/1/16 0016 17:17
# @Author  : Arliki
# @email   : hkdnxycz@outlook.com
# @File    : ip_pool
import requests
import time
import re
import random
import pandas as pd
import telnetlib


def get_ip_pool(page):
    headr = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Connection': 'keep-alive',
        'Cookie': 'channelid=0; sid=1516092300798529; _ga=GA1.2.1083811202.1516093976; _gid=GA1.2.1476790414.1516093976; Hm_lvt_7ed65b1cc4b810e9fd37959c9bb51b31=1516093976,1516093993,1516094040; Hm_lpvt_7ed65b1cc4b810e9fd37959c9bb51b31=1516094057',
        'Host': 'www.kuaidaili.com',
        'Referer': 'https://www.kuaidaili.com/free/inha/',
        'Upgrade-Insecure-Requests': '1',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
    }
    # try:
    #     ip_all = pd.read_csv('./ip_pool.csv', header=None, index_col=None, encoding='utf-8')
    #     ip_all = ip_all.loc[:, ]
    #     choce = random.randint(0, len(ip_all))
    #     ip_host = "http://" + ip_all.loc[choce,][0] + ":" + str(ip_all.loc[choce,][1])
    #     ip_tmp = {'http': ip_host}
    #     all = requests.get('https://www.kuaidaili.com/free/inha/%d/' % page, headers=headr, proxies=ip_tmp)
    # except:
    all = requests.get('https://www.kuaidaili.com/free/inha/%d/' % page, headers=headr)
    return all


def run(page, size):
    global msg
    all = get_ip_pool(page)
    # try:
    iplist = re.findall('<td data-title="IP">(.*?)</td>[\s\r\n]*<td data-title="PORT">(.*?)</td>', all.text)
    if 0 < len(iplist) < size:
        msg = 'is end'
    elif len(iplist) == 0:
        print(page, 'is wrong \non Re execute... ...')
        time.sleep(2)
        run(page, size)
    else:
        msg = 'is ok'
    for i in iplist:
        ip_host = "http://" + i[0] + ":" + str(i[1])
        ip_tmp = {'http': ip_host}
        try:
            if requests.get('http://test.arliki.com', proxies=ip_tmp, timeout=2).status_code == 200:
                with open("a.txt", "a+", encoding='utf-8')as f:
                    f.write(ip_host)
            print(ip_host, "is ok")
        except:
            print(ip_host, "is wrong")
            # try:
            #     telnetlib.Telnet(ip_host, port=ip_port, timeout=3.5)
            #     save = pd.DataFrame(iplist)
            #     save.to_csv('ip_pool.csv', index=False, header=False, mode='a+')
            # except:
            #     pass
            # except:
            #     msg = 'is wrong'


if __name__ == '__main__':
    msg = ''
    for i in range(1, 50):
        run(i, 15)
        # ts = random.randrange(3) + round(random.random(), 2)
        # time.sleep(ts)
        if msg == 'is ok':
            print(i, msg)
        elif msg == 'is end':
            print('网站已经采集完毕')
            exit()
        else:
            print(i, msg)
